package zhongbo.java.air.common.util;

import java.lang.reflect.Field;
//用于判断一个对象，除了id的其他属性是否为空
public class ObjectIsNullUtil {

    public static boolean allNonIdFieldsNonNull(Object obj) {
        if (obj == null) {
            return false;
        }
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!"id".equals(field.getName())) {
                field.setAccessible(true);
                try {
                    if (field.get(obj) == null) {
                        return false;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static class ExampleObject {
        private Integer id;
        private String name;
        private Integer age;

        public ExampleObject(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    }

    public static void main(String[] args) {
        ExampleObject obj1 = new ExampleObject(1, "John", 25);
        ExampleObject obj2 = new ExampleObject(2, null, 30);

        System.out.println(allNonIdFieldsNonNull(obj1)); 
        System.out.println(allNonIdFieldsNonNull(obj2)); 
    }
}    